/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.misc;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;


/**
 *  IO utilities.
 */
public class IO {

	
	/**
	 * Static utility library.
	 */
	private IO() {
	}
	
	
	/**
	 * Conveniently closes IO resources.
	 * 
	 * <p>The proper, yet ugly, way to do IO in Java:</p>
	 * 
	 * <pre>
	 * Reader r;
	 * try {
	 * 	   r = new FileReader(filename);
	 *     doSomeWork(r);
	 * } finally {
	 *     try {
	 *          r.close()
	 *     } catch (IOException e) {
	 *     }
	 * }
	 * </pre>
	 * 
	 * <p>That finally block is tedious, because it's so common.
	 * Using this method, you can do this instead:
	 * 
	 * <pre>
	 * Reader r;
	 * try {
	 *     r = new FileReader(filename);
	 *     doSomeWork(r);
	 * } finally {
	 *     IO.close(r);
	 * }
	 * </pre>
	 *
	 * Elimates a pet peeve of mine.
	 * 
	 * @param  r  the resource to close; may be null, 
	 *   in which case nothing happens
	 */
	public static void close(Closeable c) {
		if (c == null) {
			return;
		}
		try {
			c.close();
		} catch (IOException e) {
		}
	}
	
	
	/**
	 * Loads a resource as text.
	 * 
	 * @param c     The class whose loader to use to get the resouce
	 * @param name  The name of the resource to return
	 * @return      That resource as a string
	 * @throws IOException   if the resource cannot be read
	 */
	public static String getResourceAsText(Class c, String name) throws IOException {
		InputStream is = c.getResourceAsStream(name);
		if (is == null) {
			throw new IOException("No such resource: " + name);
		}
		InputStreamReader isr = new InputStreamReader(is);
		return read(isr);
	}


	/**
	 * Returns the file's name without any extension.
	 * 
	 * @param file  the file whose name to return
	 * @return      that file's name without any extension
	 */
	public static String baseName(File file) {
		String name = file.getName();
		int p = name.lastIndexOf('.');
		if (p >= 0) {
			return name.substring(0,p);
		} else {
			return name;
		}
	}


	public static String read(Reader reader) throws IOException {
		BufferedReader br = new BufferedReader(reader, 1024);
		try {
			char[] ch = new char[1024];
			StringBuilder result = new StringBuilder();
			for (int n = br.read(ch); n > 0; n = br.read(ch)) {
				result.append(ch, 0, n);
			}
			return result.toString();
		} finally {
			close(br);
		}
	}

}
